<template>
  <el-dialog
    :title="!dataForm.fileId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="文件名称" prop="fileName">
      <el-input v-model="dataForm.fileName" placeholder="文件名称"></el-input>
    </el-form-item>
    <el-form-item label="上传文件">
      <el-upload
        class="upload-demo"
        action="#"
        ref="upload"
        :show-file-list="true"
        :http-request="httpRequest"
        :file-list="fileList"
        :auto-upload="false"
        :limit="1"
        :on-remove="handleRemove"
        :on-exceed="handleExceed"
        >
        <el-button size="small" type="primary">点击上传</el-button>
        <div slot="tip" v-if="isUpload">
          <font color="red">上传文件不能为空</font>
        </div>
      </el-upload>
    </el-form-item>
    <el-form-item label="是否收藏" prop="isRestore">
        <el-radio-group v-model="dataForm.isRestore">
          <el-radio :label="0" >否</el-radio>
          <el-radio :label="1" >是</el-radio>
        </el-radio-group>
    </el-form-item>
    <el-form-item label="文件类型" prop="fileType">
      <el-input v-model="dataForm.fileType" placeholder="文件类型"></el-input>
    </el-form-item>
    <el-form-item label="文件夹" prop="dirId">
      <el-select v-model="dataForm.dirId" clearable placeholder="所属文件夹">
          <el-option 
          v-for="item in dirs"
          :key="item.dirId"
          :label="item.dirName"
          :value="item.dirId">
          </el-option>
        </el-select>
      <!-- <el-input v-model="dataForm.dirId" placeholder="所属文件夹id"></el-input> -->
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        fileList: [],
        visible: false,
        dataForm: {
          fileId: 0,
          fileName: '',
          fileType: '',
          fileUrl: '',
          isRestore: '',
          dirId: '',
          realName: ''
        },
        isUpload : false,
        uploadUrl: '',
        dirs: [],
        
        dataRule: {
          fileName: [
            { required: true, message: '文件名称不能为空', trigger: 'blur' }
          ],
          isRestore: [
            { required: true, message: '是否收藏不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.isUpload = false;
        this.fileList = [];
        this.uploadUrl = this.$http.adornUrl(`/upload/file`)
        this.dataForm.fileId = id || 0
        this.$http({
              url: this.$http.adornUrl(`/generator/mkdir/selectAll`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dirs = data.mkdir
                this.visible = true
              }
        }).then(
          this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.fileId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/file/info/${this.dataForm.fileId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.fileName = data.file.fileName
                this.dataForm.fileType = data.file.fileType
                this.dataForm.fileUrl = data.file.fileUrl
                this.dataForm.isRestore = data.file.isRestore
                this.dataForm.dirId = data.file.dirId
                var fileUrl = data.file.fileUrl
                this.dataForm.realName = data.file.realName;
                var realName = data.file.realName;
                this.fileList.push({
                        name: realName,
                        url: fileUrl
                })
              }
            })
          }
        })
        )
      },
      // 表单提交
      dataFormSubmit () {
        this.isUpload =false;
        this.$refs.upload.submit();
        if(this.dataForm.fileId){
          this.fileUploadSubmit();
        }
        if(this.dataForm.fileUrl == '' && this.dataForm.realName == ''){
            this.isUpload =true;
            return;
        }
      },
      handleRemove(file, fileList) {
        this.fileList = fileList;
        this.isUpload = false;
        this.dataForm.fileUrl = ''
        this.dataForm.realName = ''
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 1 个文件,请删除上个文件再进行上传`);
      },
      httpRequest(params){
        console.log('httpRequest',params);
        var dirId = this.dataForm.dirId;
        let formData = new FormData();
        formData.append('file', params.file)
        this.$http({
          url: this.uploadUrl,
          method: 'post',
          data: formData,
          headers: {
            "Content-Type": "multipart/form-data"
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.fileUrl = data.file.url;
            this.dataForm.realName = data.file.name;
            console.log("this.dataForm.realName123", this.dataForm.realName)
          } else {
            this.$message.error(data.msg)
          }
        }).then( ()=>{
          this.fileUploadSubmit(); 
        }

        )
      },
      fileUploadSubmit(){
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/file/${!this.dataForm.fileId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'fileId': this.dataForm.fileId || undefined,
                'fileName': this.dataForm.fileName,
                'fileType': this.dataForm.fileType,
                'fileUrl': this.dataForm.fileUrl,
                'isRestore': this.dataForm.isRestore,
                'realName': this.dataForm.realName,
                'dirId': this.dataForm.dirId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
      
    }
  }
</script>
